<?php

namespace app\command\controller;

use app\common\func\Constant;
use app\common\func\Lib;
use app\common\func\Libs;
use app\common\func\Tools;
use app\common\model\FlowInfo;
use app\common\model\StandardRecord;
use fast\Http;
use think\Controller;

class Jl extends Controller{

    private $returnMsg = '{"ret_code":"00","ret_msg":"SUCCESS"}';
    private $baseUrl = "./trade_log/jl/";

    public function order(){
        $data = request()->param();

        file_put_contents($this->baseUrl."order_log".date("Ym").".txt",json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);

        if (is_string($data)) {
            $data = json_decode($data, true);
        }
        if (is_array($data)) {
            $data = $data;
        }

        $sn = $data['pos_sn'];
        $equip = Lib::getEquip($sn);
        if($equip){
            try {
                $amount = bcdiv($data['amount'],'100',2);
                $transactionFee = bcdiv($data['fee_amount'],'100',2);
                $rate = round(bcdiv($transactionFee,$amount,6) * 100,2);

                $userStatus = false;
                $ids = Tools::getUserChild(38,4);
                $ids = array_merge([38],$ids);
                if(in_array($equip->main_id,$ids)){
                    $userStatus = true;
                }

                $fileName = "status.txt";
                $status = 1;
                if(!$userStatus){
                    if (!file_exists($this->baseUrl . $fileName)) {
                        $pageInfo = ["status" => $status];
                        file_put_contents($this->baseUrl . $fileName, json_encode($pageInfo));
                    }else{
                        $pageInfo = json_decode(file_get_contents($this->baseUrl . $fileName), true);
                        $status = $pageInfo['status'];
                        $pageInfo = ["status" => $status >= 10 ? 1 : $status+1];
                        file_put_contents($this->baseUrl . $fileName, json_encode($pageInfo));
                    }
                }

                if($userStatus || $status >= 10) {
                    $jl = new \app\common\model\transaction\Jl();
                    $jl->agent_id = $data['agent_id'];
                    $jl->busi_type = $data['busi_type'];
                    $jl->busi_sub_type = $data['busi_sub_type'];
                    $jl->trade_num = $data['trans_id'];
                    $jl->order_num = $data['order_id'];
                    $jl->sn = $data['pos_sn'];
                    $jl->term_no = $data['term_no'];
                    $jl->trading_time = strtotime($data['trans_time']);
                    $jl->transaction_amount = $amount;
                    $jl->transaction_rate = $rate;
                    $jl->transaction_fee = $transactionFee;
                    $jl->transaction_type = $data['pay_type'];
                    $jl->card_no = $data['card_no'];
                    $jl->ret_code = $data['ret_code'];
                    $jl->ret_msg = $data['ret_msg'];
                    $jl->gather_type = "NORMAL";
                    $jl->tran_type = Tools::getTranType('jl',$data['pay_type']);
                    $jl->save();
                }

                echo $this->returnMsg;

            }catch (\Exception $e){
                file_put_contents($this->baseUrl."order_log".date("Ym").".txt","errorInfo:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."order_log".date("Ym").".txt","data:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }else{
            $url = "http://community-system.fdkj.top/command/jl/order";
            Http::post($url,$data);
        }
    }

    public function bind(){
        $data = request()->param();
        file_put_contents("bind_log.txt","data:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);

        if (is_string($data)) {
            $data = json_decode($data, true);
        }
        if (is_array($data)) {
            $data = $data;
        }

        $sn = $data['possn'];
        $equip = Lib::getEquip($sn);
        if($equip){
            try {
                $bindTime = strtotime($data['bindTime']);

                $equip->user_info = $data['name'];
                $equip->term_no = $data['termNo'];
                $equip->merchant_no = $data['merchNo'];
                $equip->active_time = $bindTime;
                $equip->binding_time = $bindTime;
                $equip->end_time = strtotime("+".Constant::$standardDay[$equip->product_id]." day",$bindTime);
                $equip->status = 1;
                $equip->save();

                echo $this->returnMsg;

                $policyStandard = Libs::getPolicyStandardsByProduct($equip->product_id);
                if(count($policyStandard) > 0){
                    $standard = $policyStandard[0];

                    $record = new StandardRecord();
                    $record->user_id = $equip->main_id;
                    $record->sn = $sn;
                    $record->start_day = $bindTime;
                    $record->end_day = $equip->end_time;
                    $record->require_money = $standard->money;
                    $record->reward = $standard->reward;
                    $record->save();
                }
            }catch (\Exception $e){
                file_put_contents($this->baseUrl."bind_log".date("Ym").".txt","errorInfo:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."bind_log".date("Ym").".txt","data:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }else{
            $url = "http://community-system.fdkj.top/command/jl/bind";
            Http::post($url,$data);
        }

    }

    public function flow(){
        $data = request()->param();
        file_put_contents("flow_log.txt",json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);

        if (is_string($data)) {
            $data = json_decode($data, true);
        }
        if (is_array($data)) {
            $data = $data;
        }

        $sn = $data['possn'];
        $equip = Lib::getEquip($sn);
        if($equip){
            try {
                $flow = new FlowInfo();
                $flow->sn = $sn;
                $flow->order_num = $data['orderId'];
                $flow->product_id = $equip->product_id;
                $flow->trading_time = strtotime($data['transTime']);
                $flow->flow_money = bcdiv($data['amount'],100,2);
                $flow->how_many = Libs::getFlowHowMany($sn) + 1;
                $flow->save();

                echo $this->returnMsg;
            }catch (\Exception $e){
                file_put_contents($this->baseUrl."flow_log".date("Ym").".txt","errorInfo:".$e->getMessage().PHP_EOL,FILE_APPEND | LOCK_EX);
                file_put_contents($this->baseUrl."flow_log".date("Ym").".txt","data:".json_encode($data).PHP_EOL,FILE_APPEND | LOCK_EX);
            }
        }else{
            $url = "http://community-system.fdkj.top/command/jl/flow";
            Http::post($url,$data);
        }
    }

}
